var myApp = new Framework7();
$(".submit").on('click', function () {
    var phone = $.trim($("#phone").val());
    var pwd = $.trim($("#password").val());
    if (phone == "" || pwd == "") {
        myApp.alert('', '手机号或密码不能为空');
    } else {
        login(phone, pwd);
    }
});
$(".register_a").on("click", function () {
    window.location.href = "register";
});
$(".forget_pwd_text").on("click", function () {
    window.location.href = "findpwd";
});

function login(phone, pwd) {
    var params = JSON.stringify({"phoneNumber": phone, "password": pwd});
    $.ajax({
        url:'conventionerController/login',
        // url: 'http://localhost/conventionerController/login',
        dataType: 'json',
        data: {"param": params},
        type: "post",
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {

            if (data.code == 200) {
                //将用户ID存入cookie
                $.cookie("user", data.data.Id);
                window.location.href = "apply";
            } else {
                myApp.alert('', '手机号或密码错误');
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            myApp.alert('', '系统错误，请稍后重试');
        }
    })
}

function setSession(phone, id) {
    $.ajax({
        url: 'session',
        dataType: 'json',
        data: {"phone": phone},
        xhrFields: {
            withCredentials: true
        },
        type: "post",
        success: function (data) {
            if (data == 200) {
                $.cookie("user", id);
                window.location.href = "apply";
            } else {
                myApp.alert('', '系统错误，请稍后重试');
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            myApp.alert('', '系统错误，请稍后重试');
        }
    })
}